import {
  createRouter,
  createWebHashHistory,
} from 'vue-router'


export const routes = [
  {
    path: '/',
    name: 'admin',
    component: () => import('@/layouts/admin.vue'),
    redirect: '/dataManagement',
    children: [],
  },
  {
    path: '/login',
    name: 'Login',
    meta: { title: '登录', },
    component: () => import('@/pages/login/index.vue'),
  },
  {
    path: '/projectList',
    name: 'projectList',
    meta: { title: '项目列表', },
    component: () => import('@/pages/projectList/index.vue'),
  },
  {
    path: '/:pathMatch(.*)*',
    name: 'NotFound',
    component: () => import('@/pages/404.vue'),
  },
]

// 自动生成路由
const pages = import.meta.glob('../pages/**/page.js', {
  eager: true,
  import: 'default',
})

const pageComps = import.meta.glob('../pages/**/index.vue')
const pageRoutes = Object.entries(pages).map(([path, meta]) => {
  const pageJSpath = path
  path = path.replace('../pages', '').replace('/page.js', '')
  path = path || '/'
  const name = path.split('/').filter(Boolean).join('-') || 'index'
  const compPath = pageJSpath.replace('page.js', 'index.vue')
  return {
    path,
    name,
    component: pageComps[compPath],
    meta
  }
})
console.log(pageRoutes, 'pageRoutes')
pageRoutes.forEach(v => {
  if (v.meta?.layoutOtherPage) {
    routes.push(v)
  } else {
    routes[0].children.push(v)
  }
});



export const router = createRouter({
  history: createWebHashHistory('/tzl-app/'),
  // history: createWebHashHistory(),
  routes,
  scrollBehavior(to, from, savedPosition) {
    if (savedPosition)
      return savedPosition

    else
      return { top: 0 }
  },
})
